/*
 *Program Name: Player.cpp
 *Written By:   Cheng-Hsiu Hsieh
 */

#include "Player.h"
#include "SharedConstants.h"
#include "corona/corona.h"
#include <GL/glut.h>
#include <math.h>

//-----------------------------------------------------------------------------
// Globals:
//-----------------------------------------------------------------------------
cPlayer gPlayer;
static float BULLET_SPEED = 0.001f;

//-----------------------------------------------------------------------------
// Functions:
//-----------------------------------------------------------------------------
cPlayer* GetPlayer()
{
  return &gPlayer;
}

cPlayer::cPlayer():cJet( 1000 , 0 , 100)
{
  mEnergy = 1000;
  mIsSpeedUp = false;
  mIsShieldUp = false;
}

cPlayer::~cPlayer()
{
}

void cPlayer::MouseMove( float x, float y )
{
  int mMouseDelay = 0;

  // retrieve the needed state variables from openGL to make the calculation
  double modelviewMatrix[16];
	double projectionMatrix[16];
	int viewport[4];
  float newX, newY;
	glGetDoublev(GL_MODELVIEW_MATRIX, modelviewMatrix);
	glGetDoublev(GL_PROJECTION_MATRIX, projectionMatrix);
	glGetIntegerv(GL_VIEWPORT, viewport);

  if ( x >= viewport[0] && x <= viewport[2] && 
		   y >= viewport[1] && y <= viewport[3])
	{
    /*
    // set the coordinates for the near clip plane
		// invert mouse Y coordinate
		float mouseX = x;
		float mouseY = viewport[3] - y; 

    double worldX, worldY, worldZ;

		// Get unprojected end point
		gluUnProject(mouseX, mouseY, 1.0f, modelviewMatrix, projectionMatrix, viewport,
					 &worldX, &worldY, &worldZ);
           */

    double worldX, worldY, worldZ;

		// Get unprojected end point
		gluUnProject(x, y, 1.0f, modelviewMatrix, projectionMatrix, viewport,
					 &worldX, &worldY, &worldZ);
  
    newX = (float) worldX;
    newY = (float) worldY;
  }


  //X cordinate, inside the box
  float fTemp;
  fTemp = kSides - getRadius();

  if ( newX > fTemp )
    setXPosition( fTemp );
  else if ( newX < -fTemp )
    setXPosition( -fTemp );
  else
    setXPosition( newX );
  
  //Y cordinate, between -20 to 580
  setYPosition( newY );
  if (getYPosition() > 595)
    setYPosition( 595.0f );
  else if (getYPosition() < -5 )
    setYPosition( -5.0f );
}

void cPlayer::shieldUp( bool bFlag )
{
  ///////////////////
  //more right here//
  ///////////////////
  if ( bFlag && !mIsSpeedUp)
  {
    mIsShieldUp = true;
  }
  else
  {
    mIsShieldUp = false;
  }
}

void cPlayer::speedUp( bool bFlag )
{
  ///////////////////
  //more right here//
  ///////////////////
  if ( bFlag && !mIsShieldUp)
  {
    mIsSpeedUp = true;
  }
  else
  {
    mIsSpeedUp = false;
  }
}

bool cPlayer::isSpeedUp()
{
  return mIsSpeedUp;
}

bool cPlayer::isShieldUp()
{
  return mIsShieldUp;
}

void cPlayer::Draw()
{
	// push a matrix and translate for this model's position.
	glMatrixMode(GL_MODELVIEW);

  glPushMatrix();
  glTranslatef( getXPosition(),
                getYPosition(),
                getZPosition() );
  
  glRotatef( 90.0f, 0.0f, 1.0f, 0.0f );
  this->Frame(0.5f, 0.5f, 0.5f);  

  glPopMatrix();
}

void cPlayer::shoot()
{
  if ( !mIsShieldUp && !mIsSpeedUp )
    this->fire( BULLET_SPEED );
}

void cPlayer::die()
{
  ////////////////
  //Dying effect//
  ////////////////
  this->setHide( true );
}

void cPlayer::reset()
{
  this->setHide( false );
  this->addScore( -100 );
  speedUp( false );
  shieldUp( false );
  setXPosition( 0.0f );
  setYPosition( 300.0f );
  setZPosition( 0.0f );
  //loosing score for punishment
}
